/*
 * Created on Aug 5, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/*
 * This software is distributed under the MIT License
 *
 * Copyright (c) 2005 Alain Becam, Paulo Lopes, Joakim Olsson, and Johan Simonsson - 2005
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this 
 * software and associated documentation files (the "Software"), to deal in the Software 
 * without restriction, including without limitation the rights to use, copy, modify, 
 * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 
 * permit persons to whom the Software is furnished to do so, subject to the following 
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies
 *  or substantial portions of the Software.
 * 
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 */

package utils;

/**
 * This facility class allow you to easily have an automatic update in you application. Just implement the ITickable interface and create your Tick (you can even have several, but they will call the same method).
 * You create a Tick giving the time between each update (in MS), and which object to call (the one which implements ITickable, often 'this'). Then just start the tick-> MyTick.start()
 * The ticker stops when onTick (the implemented method) return everything else than 0.
 * 
 * @author Alain Becam
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Tick extends Thread
{

    Object myCaller;
    int TimeForOneTick;
    /**
     * 
     */
    public Tick(int TimeToWait, Object caller)
    {
        super();
        TimeForOneTick = TimeToWait;
        myCaller = caller;
        // TODO Auto-generated constructor stub
    }
    
    

    /* (non-Javadoc)
     * @see java.lang.Runnable#run()
     */
    public void run()
    {
        // TODO Auto-generated method stub
        try{
            Thread.sleep(TimeForOneTick);
            while ( ((ITickable )myCaller).onTick() == 0)
            {
                Thread.sleep(TimeForOneTick);
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
